import _ from 'lodash';
import * as api from '@/services/ValueApprovelMgr';
import * as uploadApi from '@/services/upload';
import * as utils from '../utils/treeUtils';

// function isEmpty(obj) {
//   const regu = '^[ ]+$';
//   const re = new RegExp(regu);
//   if (
//     obj === 'null' ||
//     obj === 'undefined' ||
//     obj === null ||
//     obj === undefined ||
//     obj === '' ||
//     re.test(obj)
//   ) {
//     return true;
//   }
//   return false;
// }

const Model = {
  namespace: 'Approve',
  state: {
    allgoodsValue: [],
    basicInfo: {},
    approvedInfo: {},
    allgoodsDifferenceReview: [],
    allgoodsValueDetail: [],
    expandedRowKeys: [],
    bulidWarnInfoList: {
      marsVersions: [],
      list: [],
    }, // 本次调价楼栋
    coreWarnInfoList: {
      marsVersions: [],
      list: [],
    }, // 核心指标
    bulletinWarnInfo: [],
    backTacticsWarnInfoList: [], // 项目背景与价格策略
    warnFlag: 0, // 是否预警状态
  },
  effects: {
    *init({ payload, callback }, { call, put }) {
      const response = yield call(api.newAllgoodsValueReviewUsingGET, payload);
      if (response && response.data && !_.isEmpty(response.data)) {
        const { data } = response;
        _.forEach(data, item => {
          const x = item;
          if (item.versionName === '调整前') {
            x.key = 0;
            x.type = 'adjust_before';
          } else if (item.versionName === '大定价版') {
            x.key = 1;
            x.type = 'pricing';
          } else if (item.versionName === '战规版') {
            x.key = 2;
            x.type = 'strategy';
          } else if (item.versionName === '投资版') {
            x.key = 3;
            x.type = 'investment';
          }
        });
        data.sort((a, b) => a.key - b.key);
        if (callback) callback(data);
        yield put({
          type: 'save',
          payload: {
            allgoodsValue: data,
          },
        });
      }
    },
    *basicInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.basicInfoUsingGET, payload);
      if (response && response.data && !_.isEmpty(response.data)) {
        const { data } = response;
        yield put({
          type: 'save',
          payload: {
            basicInfo: data,
          },
        });
      }
      if (callback) callback(response && response.data ? response.data : []);
    },
    *approvedInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.approvedInfoUsingGET, payload);
      if (response?.code === 1 && !_.isEmpty(response.data)) {
        const { data } = response;
        yield put({
          type: 'save',
          payload: {
            approvedInfo: data,
          },
        });
        if (callback) callback(data);
      }
    },
    *allgoodsValueDetail({ payload, callback }, { call, put }) {
      const response = yield call(api.newAllgoodsValueDetailUsingGET, payload);
      if (!_.isEmpty(response.data)) {
        const { data } = response;
        const expandedRowKeys = utils.getExpandedRowKeys(data);
        const treeData = utils.buildTree(data);
        yield put({
          type: 'save',
          payload: {
            allgoodsValueDetail: treeData,
            expandedRowKeys,
          },
        });
      }
      if (callback) callback();
    },
    *requestWarnInfo({ payload, projectType }, { call, put, select }) {
      const response = yield call(api.valueApprovelWarnInfoGET, payload);
      if (response?.code === 1 && !_.isEmpty(response.data)) {
        const newData = response?.data;
        // 本次调价楼栋
        const bulidWarnInfoList = {
          marsVersions: newData.bulidWarnInfoList?.map(item => item.marsVersion),
          list: newData.bulidWarnInfoList,
        };
        // 核心指标
        const coreWarnInfoList = {
          marsVersions: newData.coreWarnInfoList?.map(item => item.marsVersion),
          list: utils.getCoreWarnInfoList(newData.coreWarnInfoList),
        };
        let initbulletinWarnInfo = [
          {
            id: 0,
            status: 'success',
            type: '暂无楼栋数据',
            content: '',
            contrastType: '',
          },
        ];
        if (!_.isEmpty(newData.bulletinWarnInfo)) {
          initbulletinWarnInfo = [...newData.bulletinWarnInfo];
        }

        // 本次调整预警简报
        const bulletinWarnInfo = utils.getBulletinWarnInfo(
          initbulletinWarnInfo,
          coreWarnInfoList.list,
        );

        yield put({
          type: 'save',
          payload: {
            bulidWarnInfoList,
            coreWarnInfoList,
            ...bulletinWarnInfo,
            backTacticsWarnInfoList: newData.backTacticsWarnInfoList,
          },
        });
      }
    },
    *approved({ payload, callback }, { call }) {
      const response = yield call(api.submitToOAUsingPOST, payload);
      if (callback) callback(response);
    },
    *deleteFile({ payload, callback }, { call }) {
      const response = yield call(uploadApi.createFileDeleteUsingDelete, payload);
      // if (response && response.data && !_.isEmpty(response.data)) {
      //   const { data } = response;
      // }
      if (callback) callback(response && response.data ? response.data : []);
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
